package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 业务层代码
 */
@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;


    //分页条件查询
    public PageResult<BrandDTO> queryBrandByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        // 分页助手
        PageHelper.startPage(page, rows);
        // 分页条件
        Example example = new Example(Brand.class);
        if(StringUtils.isNotBlank(key)){
            Example.Criteria criteria = example.createCriteria();
            // 品牌名称  ： or name like %key%
            criteria.orLike("name", "%" + key + "%");
            // 首字母   :   or  letter = key
            criteria.orEqualTo("letter", key);
        }
        // 拼接排序的内容  order by     id desc|asc
        example.setOrderByClause(sortBy + " " + (desc ? "DESC" : "ASC"));
        //  分页查询
        List<Brand> brandList = brandMapper.selectByExample(example);//  select * from 表  where 条件  order by id asc|desc
        //  判断分页结果
        if(CollectionUtils.isEmpty(brandList)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        //  封装分页结果
        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);
        long total = pageInfo.getTotal();// 总记录数
        //把po转成dto
        List<BrandDTO> brandDTOList = BeanHelper.copyWithCollection(pageInfo.getList(), BrandDTO.class);
        //封装分页对象
        PageResult<BrandDTO> pageResult = new PageResult<>(total, brandDTOList);
        //  返回
        return pageResult;
    }

    // 保存品牌，加入事务
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveBrand(BrandDTO brandDTO, List<Long> cids) {
        // 先把dto转成po
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        brand.setId(null);
        // 保存数据库
        int count = brandMapper.insertSelective(brand);
        // 判断
        if(count != 1){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        // 插入中间表： INSERT INTO tb_category_brand (category_id,brand_id) VALUES (1,1),(1,2)
        int i = brandMapper.insertCategoryBrand(brand.getId(), cids);
        // 如果插入的值和分类的id数量不匹配抛异常
        if(i != cids.size()){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 根据分类id查询品牌
     * @param cid
     * @return
     */
    public List<BrandDTO> queryBrandByCategoryId(Long cid) {
        List<Brand> brandList = brandMapper.queryBrandByCategoryId(cid);
        if(CollectionUtils.isEmpty(brandList)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brandList, BrandDTO.class);
    }

    /**
     * 根据id 查询品牌
     * @param id
     * @return
     */
    public BrandDTO queryBrandById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return BeanHelper.copyProperties(brand, BrandDTO.class);
    }

    public List<BrandDTO> queryBrandByIds(List<Long> ids) {
        List<Brand> brandList = brandMapper.selectByIdList(ids);
        if(CollectionUtils.isEmpty(brandList)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);

        }
        return BeanHelper.copyWithCollection(brandList, BrandDTO.class);
    }
}
